package com.wb.mas.ui.splash;

import android.content.DialogInterface;
import android.os.Bundle;

import androidx.appcompat.app.AlertDialog;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.blankj.utilcode.util.SPUtils;
import com.wb.mas.BR;
import com.wb.mas.R;
import com.wb.mas.app.AppViewModelFactory;
import com.wb.mas.app.SPKeys;
import com.wb.mas.databinding.ActSplashBinding;
import com.wb.mas.widget.PrivacyDialog;

import io.reactivex.disposables.CompositeDisposable;
import me.goldze.mvvmhabit.base.BaseActivity;

/**
 * Copyright (C) 2018
 * SplashActivity
 * <p>
 * Description
 *
 * @author Administrator
 * @version 1.0
 * <p>
 * Ver 1.0, 2019/4/28 0028, Administrator, Create file
 */
public class SplashActivity extends BaseActivity<ActSplashBinding, SplashViewModel> {

    private CompositeDisposable compositeDisposable = new CompositeDisposable();

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.act_splash;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        initView();
    }

    @Override
    public SplashViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用LoginViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(SplashViewModel.class);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.privacyEvent.observe(this, new Observer() {
            @Override
            public void onChanged(Object o) {
                showPrivacyDialog();
            }
        });
    }

    private void initView() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (compositeDisposable != null) {
            compositeDisposable.clear();
        }
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }





    private void showPrivacyDialog() {
        PrivacyDialog privacyDialog = new PrivacyDialog(this);
        privacyDialog.setCancelable(false);
        privacyDialog.setCanceledOnTouchOutside(false);
        privacyDialog.setOnPricyClickLister(new PrivacyDialog.OnPricyClickLister() {

            @Override
            public void onTolakClick() {
                new AlertDialog.Builder(SplashActivity.this)
                        .setTitle("Perhatian")
                        .setMessage("Pernyataan Penggunaan Data\n" +
                                "Untuk menghindari tindak penipuan, maka kami perlu melakukan pengumpulan beberapa data dari perangkat Anda guna menilai resiko.\n" +
                                "Sebagian data memerlukan otorisasi Anda dan kami baru bisa mengaksesnya (Jika Anda menolak memberikan otorisasi, maka program ini tidak dapat beroperasi dengan normal).\n" +
                                "Data yang kami perlukan adalah termasuk namun tidak terbatas pada:\n" +
                                "- Kode Perangkat, ID Perangkat dan informasi mengenai perangkat lainnya\n" +
                                "- Nomor Telepon\n" +
                                "- Data Lokasi GPS\n" +
                                "- Inat/Alamat MAC\n" +
                                "- Catatan Panggilan\n"+
                                "Tekan tombol otorisasi untuk segera mengotorisasi\n" +
                                "Tekan tombol tolak untuk keluar dari program\n")
                        .setCancelable(false)
                        .setPositiveButton("Otorisasi", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dismissDialog();
                                viewModel.go();
                                SPUtils.getInstance().put(SPKeys.SP_PRIVACY, true);
                            }
                        })
                        .setNegativeButton("Tolak", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface sdialog, int which) {
                                dismissDialog();
                                finish();

                            }
                        }).show();
            }
        });
        privacyDialog.show();
    }
}
